package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.BizMsgInfoMapper;
import cc.rengu.oltp.service.common.entity.BizMsgInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务报文信息表操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class BizMsgInfoMapperImpl implements BizMsgInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public BizMsgInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertBizMsgInfo(BizMsgInfo bizMsgInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBizMsgInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bizMsgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记业务报文信息失败,instId:<{}>,channelId:<{}>,transSeqNum:<{}>,createTime:<{}>,iReturnCode:<{}>", bizMsgInfo.getInstId(), bizMsgInfo.getChannelId(), bizMsgInfo.getTransSeqNum(), bizMsgInfo.getCreateTime(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<BizMsgInfo> selectBizMsgInfoByTransSeqNum(String instId, String channelId, String transSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBizMsgInfoByTransSeqNum");
        BizMsgInfo bizMsgInfo = new BizMsgInfo();
        bizMsgInfo.setInstId(instId);
        bizMsgInfo.setChannelId(channelId);
        bizMsgInfo.setTransSeqNum(transSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bizMsgInfo, BizMsgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据交易流水号获取业务报文信息失败,instId:<{}>,channelId:<{}>,transSeqNum:<{}>", instId, channelId, transSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (BizMsgInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateBizMsgInfoByTransSeqNum(BizMsgInfo bizMsgInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBizMsgInfoByTransSeqNum");
        bizMsgInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bizMsgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新业务报文信息失败,instId:<{}>,channelId:<{}>,sysSeqNum:<{}>,createTime:<{}>,iReturnCode:<{}>", bizMsgInfo.getInstId(), bizMsgInfo.getChannelId(), bizMsgInfo.getTransSeqNum(), bizMsgInfo.getCreateTime(), iReturnCode);
        }
        return iReturnCode;
    }
}
